package vn.com.aptech.msh.controller;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PostConstruct;
import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import vn.com.aptech.msh.utils.FileUploadHelper;
import vn.com.aptech.msh.business.IEstateBean;
import vn.com.aptech.msh.criteria.RentalBasisCriteria;
import vn.com.aptech.msh.entities.Advertising;
import vn.com.aptech.msh.entities.RentalBasisAccommodation;
import vn.com.aptech.msh.entities.User;
import vn.com.aptech.msh.utils.NavigationHelper;
import vn.com.aptech.msh.utils.PaginationHelper;
import vn.com.aptech.msh.utils.ResourceBundleHelper;

/**
 *
 * @author trietto
 */
@Named("rentalBasisController")
@ConversationScoped
public class RentalBasisController implements Serializable {

    private final int PAGE_SIZE = 5;
    @Inject
    Conversation conversation;
    @Inject
    IEstateBean estateBusiness;
    private RentalBasisCriteria criteria;
    @Inject
    @Named("currentUser")
    User currentUser;
    private Advertising model;
    private List<Advertising> listing;
    private PaginationHelper<Advertising> pagination;

    @PostConstruct
    public void init() {
        initConversation();
        prepareData();
        leechSearchResult();
    }

    public void prepareData() {
        model = new Advertising();
        model.setRentalBasisAccommodation(new RentalBasisAccommodation());
        criteria = new RentalBasisCriteria();
    }

    public void initConversation() {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }

    public String advertise() {
        try {
            model.setPostDate(Calendar.getInstance().getTime());
            model.setPriority(0);
            model.setUser(currentUser);
            model = estateBusiness.advertiseRentalBasis(model);
            return NavigationHelper.toAdvertiseSuccessPage(model.getAdvertisingID());
        } catch (Exception ex) {
            Logger.getLogger(RentalBasisController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, ResourceBundleHelper.get("advertise.failed"), "");
            FacesContext.getCurrentInstance().addMessage("", msg);
            return null;
        }
    }

    public void handleImageUpload(FileUploadEvent event) {
        try {
            FacesContext ctx = FacesContext.getCurrentInstance();
            UploadedFile uf = event.getFile();
            Logger.getLogger(RentalBasisController.class.getName()).log(Level.INFO, "Uploaded: {0}", event.getFile().getFileName());
            String path = FileUploadHelper.getSaveImagePath(ctx, currentUser);
            FileUploadHelper.saveFile(uf, path);
            String relativePath = FileUploadHelper.getImagePath(currentUser) + uf.getFileName();
            model.setImagePath(relativePath);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "File Uploaded Successfully", event.getFile().getFileName() + " is uploaded");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        } catch (Exception ex) {
            Logger.getLogger(RentalBasisController.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error while uploading", null);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }

    public void leechSearchResult() {
        listing = estateBusiness.searchRentalBasis(criteria);
        pagination = new PaginationHelper<Advertising>(PAGE_SIZE, listing);
    }

    public String search() {
        leechSearchResult();
        return "listing";
    }

    public RentalBasisCriteria getCriteria() {
        return criteria;
    }

    public void setCriteria(RentalBasisCriteria criteria) {
        this.criteria = criteria;
    }

    public Advertising getModel() {
        return model;
    }

    public void setModel(Advertising model) {
        this.model = model;
    }

    public PaginationHelper<Advertising> getPagination() {
        return pagination;
    }

    public void setPagination(PaginationHelper<Advertising> pagination) {
        this.pagination = pagination;
    }
}
